package com.bupt.uchouten.client.linkage;

import com.bupt.uchouten.common.block.entity.TenkaiVigorousVineBlockEntity;
import com.bupt.uchouten.common.linkage.TenkaiVVInnerPixelmonBlockEntity;
import com.mojang.blaze3d.vertex.PoseStack;
import com.pixelmonmod.pixelmon.api.util.helpers.QuaternionHelper;
import com.pixelmonmod.pixelmon.client.models.pokeballs.ModelPokeballs;
import com.pixelmonmod.pixelmon.client.models.smd.AnimationType;
import com.pixelmonmod.pixelmon.client.render.GenericModelHolder;
import com.pixelmonmod.pixelmon.client.render.tileEntities.TileEntityRenderer;
import net.minecraft.client.renderer.MultiBufferSource;
import net.minecraft.client.renderer.blockentity.BlockEntityRendererProvider;
import net.minecraft.world.level.block.state.BlockState;

import java.util.Optional;

/**
 * @author wujichen158
 */
public class TenkaiVVInnerPixelmonRenderer extends TileEntityRenderer<TenkaiVigorousVineBlockEntity> {

    public TenkaiVVInnerPixelmonRenderer(BlockEntityRendererProvider.Context dispatcher) {
        super(dispatcher);
    }

    @Override
    public void render(TenkaiVigorousVineBlockEntity blockEntity, BlockState state, double x, double y, double z, float partialTicks, PoseStack matrix, MultiBufferSource buffer, int combinedLight, int combinedOverlay) {
        TenkaiVVInnerPixelmonBlockEntity innerBlockEntity = blockEntity.getInnerPixelmonBlockEntity();

        if (innerBlockEntity.isStayLight()) {
            innerBlockEntity.setRotation(0F);
        }

        if (innerBlockEntity.isBeingUsed()) {
            for (int slot = 0; slot < innerBlockEntity.getPokeballType().length; ++slot) {
                if (Optional.ofNullable(innerBlockEntity.getPokeballType()[slot]).isPresent()) {
                    this.renderPokeball(matrix, buffer, combinedLight, combinedOverlay, slot, innerBlockEntity);
                }
            }
        }
    }

    private void renderPokeball(PoseStack matrix, MultiBufferSource buffer, int light, int overlay, int slot, TenkaiVVInnerPixelmonBlockEntity blockEntity) {
        matrix.pushPose();
        // x并列两侧，y递减，z相同。左偶右奇
        float z = 0F;
        switch (slot) {
            case 0:
                matrix.translate(-0.25, -1.75, z);
                break;
            case 1:
                matrix.translate(0.85, -1.4, z);
                break;
            case 2:
                matrix.translate(-0.65, -1.05, z);
                break;
            case 3:
                matrix.translate(0.79, -0.8, z);
                break;
            case 4:
                matrix.translate(-0.85, -0.3, z);
                break;
            case 5:
                matrix.translate(0.85, -0.15, z);
                break;
            default:
                break;
        }

        float color = 1F;
        float alpha = 1F;
        if (blockEntity.isAllPlaced()) {
            if (blockEntity.flashTimer < 6 || blockEntity.isStayLight()) {
                alpha = 0.5F;
            }

            if (blockEntity.flashTimer >= 10) {
                blockEntity.flashTimer = 0;
            }
        }

        GenericModelHolder<? extends ModelPokeballs> pokeball = blockEntity.pokeballType[slot].getModelHolder();
        matrix.mulPose(QuaternionHelper.fromEuler(blockEntity.rotation + 180.0F, 0.0F, 1.0F, 0.0F));
        matrix.mulPose(QuaternionHelper.fromEuler(180.0F, 1.0F, 0.0F, 0.0F));
        ModelPokeballs model = pokeball.getModel();
        model.theModel.setAnimation(AnimationType.IDLE);
        matrix.scale(0.0033333334F, 0.0033333334F, 0.0033333334F);
        this.renderSMD(matrix, buffer, model, blockEntity.pokeballType[slot].getModelTexture(), light, overlay, color, color, color, alpha);
        matrix.popPose();
    }
}
